﻿using System;
using System.Collections.Generic;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model;
using Newtonsoft.Json.Converters;
using NLog.Fluent;

namespace Labthink.PCSoft.UniversalHost4
{
    /// <summary>
    ///  试验流程控制
    /// </summary>
    public class TestFlow
    {
        /// <summary>
        /// 当前参数信息
        /// </summary>
        public Dictionary<int, List<FullData>> CurrentParams;

        protected readonly IsoDateTimeConverter TimeFormat = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };

        public TestFlow()
        {
            CurrentParams = new Dictionary<int, List<FullData>>();
        }

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="hasSub">是否包含并列试验</param>
        /// <param name="dataSn">杯（腔）号，默认1</param>
        /// <param name="testType">试验类型</param>
        /// <param name="testId">试验id</param>
        /// <param name="start"></param>
        /// <param name="sn"></param>
        public void CreateDb(int hasSub, int? dataSn, int testType, int testId, DateTime start, string sn)
        {
            if (dataSn == null) dataSn = 1;
            var db = new DataBar { Datas = CurrentParams[dataSn.Value], DataSn = dataSn.Value, TestType = testType, TestId = testId };
            CurrentRepository.StartTest(hasSub, db, start, sn);
        }

        /// <summary>
        /// 创建数据库
        /// </summary>
        /// <param name="hasSub">是否包含并列试验</param>
        /// <param name="dataSn">杯（腔）号，默认1</param>
        /// <param name="testType">试验类型</param>
        /// <param name="testId">试验id</param>
        /// <param name="start"></param>
        /// <param name="sn"></param>
        /// <param name="dbKey">数据库模标识，用于使用指定的数据库模板，dbkey作为文件名位于默认数据库路径下。比如dbkey=o2,则使用harddisk\bootstrap\config\temp\default\o2下的数据库模板</param>
        public void CreateDb(int hasSub, int? dataSn, int testType, int testId, DateTime start, string sn, string dbKey)
        {
            if (dataSn == null) dataSn = 1;
            var db = new DataBar { Datas = CurrentParams[dataSn.Value], DataSn = dataSn.Value, TestType = testType, TestId = testId };
            CurrentRepository.StartTest(hasSub, db, start, sn, dbKey);
        }

        /// <summary>
        /// 发送试验数据
        /// </summary>
        /// <param name="dataSn"></param>
        /// <param name="testType"></param>
        /// <param name="testId"></param>
        /// <param name="datas"></param>
        public void SendTestData(int? dataSn, int testType, int testId, List<FullData> datas)
        {
            if (dataSn == null) dataSn = 1;

            var db = new DataBar { Datas = datas, DataType = 2, DataSn = dataSn.Value, TestType = testType, TestId = testId };
            //CurrentRepository.Conversion(datas);
            CurrentRepository.AddData(db);
        }

        /// <summary>
        /// 发送试验结果
        /// </summary>
        /// <param name="dataSn"></param>
        /// <param name="testType"></param>
        /// <param name="testId"></param>
        /// <param name="results"></param>
        public void SendTestResult(int? dataSn, int testType, int testId, List<FullData> results)
        {
            if (dataSn == null) dataSn = 1;

            var db = new DataBar { Datas = results, DataType = 3, DataSn = dataSn.Value, TestType = testType, TestId = testId };

            //CurrentRepository.Conversion(results);
            CurrentRepository.AddResult(db);
        }

        /// <summary>
        /// 保存试验数据
        /// </summary>
        public void SaveTest()
        {
            CurrentRepository.SaveTest(1);
        }

        /// <summary>
        /// 保存试验数据
        /// </summary>
        /// <param name="dataSn"></param>
        public void SaveTest(int dataSn)
        {
            CurrentRepository.SaveTest(dataSn);
        }

        /// <summary>
        /// 移除试验数据
        /// </summary>
        /// <param name="testId"></param>
        protected void RemoveTest(int testId)
        {
            CurrentRepository.RemoveTest(testId);
        }
    }
}
